package com.demarc.pesquisas.infraestrutura.util.relatorio;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang3.StringUtils;

import com.demarc.pesquisas.infraestrutura.excecao.InfraException;

/**
 * A report class that generates PDF and XLS files from a list of values.
 */
public class Report {

    public static final Locale LOCALE_BRAZIL = new Locale("pt", "BR");
    private static final Integer DEFAULT_HEADER_WIDTH = 40;

    private final String title;
    private String applicationTitle;
    private final Locale locale;
    private int fontSize;
    private final List<ReportHeader> headers;
    private final List<ReportRow> rows;
    private boolean landscape;
    private String footer;
    private short rowHeight;

    public Report(String title, Locale locale) {
        this.title = title;
        this.locale = locale;
        this.footer = StringUtils.EMPTY;
        this.headers = new ArrayList<ReportHeader>();
        this.rows = new ArrayList<ReportRow>();
        this.fontSize = 10;
        this.rowHeight = 12;
    }

    public String getApplicationTitle() {
        return applicationTitle;
    }

    public void setApplicationTitle(String applicationTitle) {
        this.applicationTitle = applicationTitle;
    }

    public String getTitle() {
        return title;
    }

    public Locale getLocale() {
        return locale;
    }

    public boolean isLandscape() {
        return landscape;
    }

    public void setLandscape(boolean landscape) {
        this.landscape = landscape;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public List<ReportHeader> getHeaders() {
        return headers;
    }

    public List<ReportRow> getRows() {
        return rows;
    }

    public String getFooter() {
        return footer;
    }

    public void setFooter(String footer) {
        this.footer = footer;
    }

    public int getFontFooterSize() {
        return 8;
    }

    public short getRowHeight() {
        return rowHeight;
    }

    public void setRowHeight(short rowHeight) {
        this.rowHeight = rowHeight;
    }

    public ReportRow addRow() {
        ReportRow row = new ReportRow(this);
        rows.add(row);
        return row;
    }

    public ReportHeader addHeader(String headerTitle) {
        return addHeader(headerTitle, DEFAULT_HEADER_WIDTH);
    }

    public ReportHeader addHeader(String headerTitle, Integer width) {
        ReportHeader header = new ReportHeader(this, headerTitle, width);
        headers.add(header);
        return header;
    }

    public String getFileName() {
        return title.replaceAll(" ", "_");
    }

    public byte[] toPDF(String templateLocation) {
        validateHeaderColumn();
        return PDFReportHelper.toPDF(this, templateLocation);
    }

    protected void validateHeaderColumn() {
        int headerCount = headers.size();

        for (ReportRow reportRow : rows) {
            if (reportRow.getColumns().size() != headerCount) {
                throw new InfraException("The column count and header count should be the same!");
            }
        }
    }

    @Override
    public String toString() {
        return title;
    }
}
